// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.assets.controller.request.ImportAssetsBudgetReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsBudgetReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsBudgetReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsBudgetYearReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsSettlementReq;
import com.liuxinlong.modules.assets.service.AssetsBudgetService;
import com.liuxinlong.modules.butler.controller.request.UpdateTrainStatusReq;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.AssetsBudget;
import com.liuxinlong.modules.entity.AssetsBudgetYear;
import com.liuxinlong.modules.entity.AssetsMonthDetail;
import com.liuxinlong.modules.entity.AssetsSettlement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固资预算控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/assets/budget")
public class AssetsBudgetController {
    @Autowired
    private AssetsBudgetService assetsBudgetService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询固资预算信息
     *
     * @param req 请求信息
     * @return 固资预算信息列表
     */
    @PostMapping("page")
    public BaseResp pageAssetsBudget(@RequestBody QueryAssetsBudgetReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("year", req.getCurrentYear());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectManage", req.getProjectManage());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("status", req.getStatus());
        Map<String, Object> dataMap= assetsBudgetService.pageAssetsBudget(queryMap);
//        int totalNum = assetsBudgetService.getAssetsBudgetCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询固资预算信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 新增固资预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增固资预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsBudget(@RequestBody UpdateAssetsBudgetReq req) {
        AssetsBudget assetsBudget = new AssetsBudget();
        assetsBudget.setPlanNo(req.getPlanNo());
        assetsBudget.setDepartment(req.getDepartment());
        assetsBudget.setProjectName(req.getProjectName());
        assetsBudget.setProjectManage(req.getProjectManage());
        assetsBudget.setProjectType(req.getProjectType());
        assetsBudget.setPlanAmount(req.getPlanAmount());
        assetsBudget.setPlanDate(req.getPlanDate());
        assetsBudget.setPlanCloseDate(req.getPlanCloseDate());
        assetsBudget.setCurrentYear(req.getCurrentYear());
        assetsBudget.setCurrentYearAmount(req.getCurrentYearAmount());
        assetsBudget.setNextYearAmount(req.getNextYearAmount());
        assetsBudget.setPlant(req.getPlant());
        assetsBudget.setRemark(req.getRemark());

        AssetsMonthDetail assetsMonthDetail = new AssetsMonthDetail();
        assetsMonthDetail.setJanAmount(req.getJanAmount());
        assetsMonthDetail.setFebAmount(req.getFebAmount());
        assetsMonthDetail.setMarAmount(req.getMarAmount());
        assetsMonthDetail.setAprAmount(req.getAprAmount());
        assetsMonthDetail.setMayAmount(req.getMayAmount());
        assetsMonthDetail.setJunAmount(req.getJunAmount());
        assetsMonthDetail.setJulAmount(req.getJulAmount());
        assetsMonthDetail.setAugAmount(req.getAugAmount());
        assetsMonthDetail.setSepAmount(req.getSepAmount());
        assetsMonthDetail.setOctAmount(req.getOctAmount());
        assetsMonthDetail.setDecAmount(req.getDecAmount());
        assetsMonthDetail.setNovAmount(req.getNovAmount());

        assetsBudgetService.addAssetsBudget(assetsBudget,assetsMonthDetail);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资预算信息成功");
        return resp;
    }

    /**
     * 修改固资预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改固资预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsBudget(@RequestBody UpdateAssetsBudgetReq req) {
        AssetsBudget assetsBudget = new AssetsBudget();
        assetsBudget.setId(req.getId());
        assetsBudget.setPlanNo(req.getPlanNo());
        assetsBudget.setDepartment(req.getDepartment());
        assetsBudget.setProjectName(req.getProjectName());
        assetsBudget.setProjectManage(req.getProjectManage());
        assetsBudget.setProjectType(req.getProjectType());
        assetsBudget.setPlanAmount(req.getPlanAmount());
        assetsBudget.setPlanDate(req.getPlanDate());
        assetsBudget.setPlanCloseDate(req.getPlanCloseDate());
        assetsBudget.setCurrentYearAmount(req.getCurrentYearAmount());
        assetsBudget.setNextYearAmount(req.getNextYearAmount());
        assetsBudget.setCurrentYear(req.getCurrentYear());
        assetsBudget.setPlant(req.getPlant());
        assetsBudget.setRemark(req.getRemark());

        AssetsMonthDetail assetsMonthDetail = new AssetsMonthDetail();
        assetsMonthDetail.setJanAmount(req.getJanAmount());
        assetsMonthDetail.setFebAmount(req.getFebAmount());
        assetsMonthDetail.setMarAmount(req.getMarAmount());
        assetsMonthDetail.setAprAmount(req.getAprAmount());
        assetsMonthDetail.setMayAmount(req.getMayAmount());
        assetsMonthDetail.setJunAmount(req.getJunAmount());
        assetsMonthDetail.setJulAmount(req.getJulAmount());
        assetsMonthDetail.setAugAmount(req.getAugAmount());
        assetsMonthDetail.setSepAmount(req.getSepAmount());
        assetsMonthDetail.setOctAmount(req.getOctAmount());
        assetsMonthDetail.setDecAmount(req.getDecAmount());
        assetsMonthDetail.setNovAmount(req.getNovAmount());
        assetsBudgetService.updateAssetsBudget(assetsBudget,assetsMonthDetail);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资预算信息成功");
        return resp;
    }

    /**
     * 删除固资预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除固资预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsBudget(@RequestBody BaseDeleteReq req) {
        assetsBudgetService.deleteAssetsBudget(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资预算信息成功");
        return resp;
    }

    /**
     * 导出固资预算信息
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出固资预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportAssetsBudget(@RequestBody QueryBaseReq req, HttpServletResponse response) {
        assetsBudgetService.exportAssetsBudget(response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入固资预算数据", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importAssetsBudget(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = assetsBudgetService.importAssetsBudget((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入固资预算信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入固资预算数据", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddAssetsBudget(@RequestBody ImportAssetsBudgetReq req) {
        return assetsBudgetService.batchAddAssetsBudget(req.getDataList());
    }

    /**
     * 修改固资预算状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "修改固资预算状态", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsBudgetStatus(@RequestBody UpdateTrainStatusReq req) {
        assetsBudgetService.updateAssetsBudgetStatus(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资预算状态成功");
        return resp;
    }

    /**
     * 查询固资预算拓展信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("expand")
    public BaseResp queryAssetsBudgetExpand(@RequestBody QueryBaseReq req) {
        Map<String,Object> dataMap = assetsBudgetService.queryAssetsBudgetExpand(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询固资预算拓展信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 查询固资年度预算信息
     *
     * @param req 请求信息
     * @return 固资年度预算信息列表
     */
    @PostMapping("year-page")
    public BaseResp pageAssetsBudgetYear(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = assetsBudgetService.pageAssetsBudgetYear(queryMap);
        int totalNum = assetsBudgetService.getAssetsBudgetYearCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询固资年度预算信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增固资年度预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("year-add")
    @LogAnnotation(value = "新增固资年度预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsBudgetYear(@RequestBody UpdateAssetsBudgetYearReq req) {
        AssetsBudgetYear assetsBudgetYear = new AssetsBudgetYear();
        assetsBudgetYear.setCurrentYear(req.getCurrentYear());
        assetsBudgetYear.setGreenAmount(req.getGreenAmount());
        assetsBudgetYear.setCostAmount(req.getCostAmount());
        assetsBudgetService.addAssetsBudgetYear(assetsBudgetYear);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资预算信息成功");
        return resp;
    }

    /**
     * 修改固资年度预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("year-update")
    @LogAnnotation(value = "修改固资年度预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsBudgetYear(@RequestBody UpdateAssetsBudgetYearReq req) {
        AssetsBudgetYear assetsBudgetYear = new AssetsBudgetYear();
        assetsBudgetYear.setId(req.getId());
        assetsBudgetYear.setCurrentYear(req.getCurrentYear());
        assetsBudgetYear.setGreenAmount(req.getGreenAmount());
        assetsBudgetYear.setCostAmount(req.getCostAmount());
        assetsBudgetService.updateAssetsBudgetYear(assetsBudgetYear);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资年度预算信息成功");
        return resp;
    }

    /**
     * 删除固资年度预算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("year-delete")
    @LogAnnotation(value = "删除固资年度预算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsBudgetYear(@RequestBody BaseDeleteReq req) {
        assetsBudgetService.deleteAssetsBudgetYear(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资年度预算信息成功");
        return resp;
    }

    /**
     * 查询固资结算信息
     *
     * @param req 请求信息
     * @return 固资结算信息列表
     */
    @PostMapping("settlement-page")
    public BaseResp pageAssetsSettlement(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = assetsBudgetService.pageAssetsSettlement(queryMap);
        int totalNum = assetsBudgetService.getAssetsSettlementCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询固资结算信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增固资结算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("settlement-add")
    @LogAnnotation(value = "新增固资结算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsSettlement(@RequestBody UpdateAssetsSettlementReq req) {
        AssetsSettlement assetsSettlement = new AssetsSettlement();
        assetsSettlement.setSettlementYear(req.getSettlementYear());
        assetsSettlement.setSettlementMonth(req.getSettlementMonth());
        assetsSettlement.setSettlementStartDate(req.getSettlementStartDate());
        assetsSettlement.setSettlementEndDate(req.getSettlementEndDate());
        assetsBudgetService.addAssetsSettlement(assetsSettlement);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资结算信息成功");
        return resp;
    }

    /**
     * 修改固资结算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("settlement-update")
    @LogAnnotation(value = "修改固资结算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsSettlement(@RequestBody UpdateAssetsSettlementReq req) {
        AssetsSettlement assetsSettlement = new AssetsSettlement();
        assetsSettlement.setId(req.getId());
        assetsSettlement.setSettlementYear(req.getSettlementYear());
        assetsSettlement.setSettlementMonth(req.getSettlementMonth());
        assetsSettlement.setSettlementStartDate(req.getSettlementStartDate());
        assetsSettlement.setSettlementEndDate(req.getSettlementEndDate());
        assetsBudgetService.updateAssetsSettlement(assetsSettlement);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资结算信息成功");
        return resp;
    }

    /**
     * 删除固资结算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("settlement-delete")
    @LogAnnotation(value = "删除固资结算信息", business = LogBusinessConstants.ASSETS_BUDGET, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsSettlement(@RequestBody BaseDeleteReq req) {
        assetsBudgetService.deleteAssetsSettlement(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资结算信息成功");
        return resp;
    }
}
